package daos;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for Actor
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see daos.Actor
 * @author MyEclipse Persistence Tools
 */

public class ActorDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ActorDAO.class);
	// property constants
	public static final String ACTOR_NAME = "actorName";
	public static final String COUNTRY = "country";
	public static final String HEIGHT = "height";
	public static final String HOBBY = "hobby";
	public static final String INTRODUCTION = "introduction";

	protected void initDao() {
		// do nothing
	}

	public void save(Actor transientInstance) {
		log.debug("saving Actor instance");
		try {
			getHibernateTemplate().save(transientInstance);
			this.getHibernateTemplate().getSessionFactory().getCurrentSession().beginTransaction().commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Actor persistentInstance) {
		log.debug("deleting Actor instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Actor findById(java.lang.Integer id) {
		log.debug("getting Actor instance with id: " + id);
		try {
			Actor instance = (Actor) getHibernateTemplate().get("daos.Actor",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Actor instance) {
		log.debug("finding Actor instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Actor instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Actor as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByActorName(Object actorName) {
		return findByProperty(ACTOR_NAME, actorName);
	}

	public List findByCountry(Object country) {
		return findByProperty(COUNTRY, country);
	}

	public List findByHeight(Object height) {
		return findByProperty(HEIGHT, height);
	}

	public List findByHobby(Object hobby) {
		return findByProperty(HOBBY, hobby);
	}

	public List findByIntroduction(Object introduction) {
		return findByProperty(INTRODUCTION, introduction);
	}

	public List findAll() {
		log.debug("finding all Actor instances");
		try {
			String queryString = "from Actor";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Actor merge(Actor detachedInstance) {
		log.debug("merging Actor instance");
		try {
			Actor result = (Actor) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Actor instance) {
		log.debug("attaching dirty Actor instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Actor instance) {
		log.debug("attaching clean Actor instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ActorDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ActorDAO) ctx.getBean("ActorDAO");
	}
}